//
//  ChatGiftCell.m
//  tcpoctest
//
//  Created by apple on 13-6-21.
//  Copyright (c) 2013年 gump. All rights reserved.
//

#import "ChatGiftCell.h"

#import <QuartzCore/QuartzCore.h>

#import "chatDataSource.h"

static CGFloat textMarginHorizontal = 15.0f;
static CGFloat textMarginVertical = 10.0f;

@implementation ChatGiftCell

-(id)initMessagingCellWithReuseIdentifier:(NSString*)reuseIdentifier {
    if (self = [super initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.balloonView = [[UIImageView alloc] initWithFrame:CGRectZero];
        
        self.timeLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        self.avatarImageView = [[UIImageView alloc] initWithImage:nil];
        
        self.avatarImageView.layer.cornerRadius = 3.0;
        self.avatarImageView.layer.masksToBounds = YES;
        self.avatarImageView.layer.borderColor = [UIColor colorWithWhite:0.0 alpha:0.2].CGColor;
        self.avatarImageView.layer.borderWidth = 1.0;
        self.avatarImageView.userInteractionEnabled = YES;
        
        
        
        self.timeLabel.font = [UIFont boldSystemFontOfSize:12.0f];
        self.timeLabel.textColor = [UIColor darkGrayColor];
        self.timeLabel.backgroundColor = [UIColor clearColor];
        
        [self.contentView addSubview: self.timeLabel];
        [self.contentView addSubview: self.avatarImageView];
        [self.contentView addSubview:self.balloonView];
        
        
        UILongPressGestureRecognizer *recognizer =
        [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];
        [recognizer setMinimumPressDuration:1.0f];
        [self addGestureRecognizer:recognizer];
        
        singleTouch = [[UITapGestureRecognizer alloc] initWithTarget:self action:nil];
        singleTouch.delegate = self;
        [self addGestureRecognizer:singleTouch];
        
        self.giftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
        [self.balloonView addSubview:self.giftImageView];
        
        self.taxtLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        self.taxtLabel.numberOfLines = 0;
        self.taxtLabel.font = [UIFont systemFontOfSize:16.0f];
        self.taxtLabel.backgroundColor = [UIColor clearColor];
        self.taxtLabel.textAlignment = UITextAlignmentLeft;
        [self.balloonView addSubview:self.taxtLabel];
        
        self.StateLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        self.StateLabel.backgroundColor = COLOR_CHATSTATE_SENDING;
        self.StateLabel.layer.cornerRadius = 2;
        self.StateLabel.textColor = [UIColor whiteColor];
        self.StateLabel.font = [UIFont boldSystemFontOfSize:10];
        self.StateLabel.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:self.StateLabel];
        
    }
    
    return self;
}

+(CGFloat)maxTextWidth {
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
        return 220.0f;
    } else {
        return 400.0f;
    }
}

-(CGSize)textSize:(NSString*)text{
    return [text sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:CGSizeMake(140, 1000) lineBreakMode:NSLineBreakByWordWrapping];

}

#pragma mark -
#pragma mark Layouting

- (void)layoutSubviews {
    
    CGSize dateSize = [self.timeLabel.text sizeWithFont:self.timeLabel.font forWidth:[ChatGiftCell maxTextWidth] lineBreakMode:NSLineBreakByWordWrapping];
    
//    CGSize textSize = self.taxtLabel.frame.size;
    
    
    CGRect ballonViewFrame = CGRectZero;
    CGRect timeLabelFrame = CGRectZero;
    CGRect avatarImageFrame = CGRectZero;
    CGRect giftFrame = CGRectZero;
    CGRect labelFrame = CGRectZero;
    CGRect stateLabelFrame = CGRectZero;
    
    timeLabelFrame = CGRectMake((WINDOW_WIDTH - dateSize.width)/2,
                                0.0f,
                                dateSize.width,
                                dateSize.height);
    
    CGSize textSize = [self textSize:self.taxtLabel.text];//用礼物文字框的size
    
    if (self.sent == YES) {
      
        ballonViewFrame = CGRectMake(self.frame.size.width - (2*textMarginHorizontal + 55.0f + 180),
                                     timeLabelFrame.size.height + BALLOONSIZE,
                                     textSize.width + 2*textMarginHorizontal + 50,
                                     textSize.height + 2*textMarginVertical + 5);
        
        avatarImageFrame = CGRectMake(self.frame.size.width - 45.0f,
                                      ballonViewFrame.origin.y + 1,
                                      40.0f, 40.0f);
        
        giftFrame = CGRectMake(10, 10, 40, 40);
        
        labelFrame = CGRectMake(55, 1, textSize.width + 10, textSize.height + 20);
        
        stateLabelFrame = CGRectMake(self.frame.size.width - (ballonViewFrame.size.width + 2*textMarginHorizontal + 54.0f),
                                     timeLabelFrame.size.height + STATESIZE,
                                     30, 12);
    }
    else{
            
        ballonViewFrame = CGRectMake(50.0f,
                                     timeLabelFrame.size.height + BALLOONSIZE,
                                     textSize.width + 2*textMarginHorizontal + 50,
                                     textSize.height + 2*textMarginVertical + 5);
               
        avatarImageFrame = CGRectMake(5.0f, ballonViewFrame.origin.y + 1, 40.0f, 40.0f);
        
        giftFrame = CGRectMake(20, 10, 40, 40);
        
        labelFrame = CGRectMake(65, 1, textSize.width + 10, textSize.height + 20);
        
    }
    
    self.balloonView.image = [self balloonImage:self.sent];
    
    self.balloonView.frame = ballonViewFrame;
    
    if (self.avatarImageView.image != nil) {
        self.avatarImageView.frame = avatarImageFrame;
    }
    
    if (self.timeLabel.text != nil) {
        self.timeLabel.frame = timeLabelFrame;
    }
    
    self.giftImageView.frame = giftFrame;
    self.taxtLabel.frame = labelFrame;
    self.StateLabel.frame = stateLabelFrame;
}

#pragma mark - 以图片的某个位置平铺整个图片
-(UIImage*)balloonImage:(BOOL)sent
{
    if (sent == YES) {
        return [[UIImage imageNamed:@"myMessage.png"] stretchableImageWithLeftCapWidth:20.0f topCapHeight:25.0f];
        
    }else{
        return [[UIImage imageNamed:@"yourMessage.png"] stretchableImageWithLeftCapWidth:20.0f topCapHeight:25.0f];
    }
}

-(void)handleTapPress
{
        
}

#pragma mark UIGestureRecognizerDelegate methods
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    return YES;
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
    CGPoint pointInCell = [gestureRecognizer locationInView:self];
    if (gestureRecognizer == singleTouch)
    {
        if(CGRectContainsPoint(self.balloonView.frame, pointInCell)) {
        [self handleTapPress];
        }
        else if(CGRectContainsPoint(self.avatarImageView.frame, pointInCell))
        {
            [self pressHeadImage];
        }
    } else {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"NOTIFICATION_CELL_PRESS" object:nil];
    }
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"NOTIFICATION_CELL_PRESS" object:nil];
    
    return YES;
}

#pragma mark -
#pragma mark UIGestureRecognizer-Handling

- (void)handleLongPress:(UILongPressGestureRecognizer *)longPressRecognizer {
    if (longPressRecognizer.state != UIGestureRecognizerStateBegan) {
        return;
    }
    
    if ([self becomeFirstResponder] == NO) {
        return;
    }
    
    
  
                UIMenuController * menu = [UIMenuController sharedMenuController];
                [menu setTargetRect:self.balloonView.frame inView:self];
                
                [menu setMenuVisible:YES animated:YES];
           
    
}

-(BOOL)canBecomeFirstResponder {
    return YES;
}

-(BOOL)canPerformAction:(SEL)action withSender:(id)sender{
    if (action == @selector(delete:)) {
      
        return YES;
            
    }
    else {
        return [super canPerformAction:action withSender:sender];
    }
}

- (void)delete:(id)sender
{
    [[DataControl shareControl].chatDS DelCurChatArrObjectByRow:self.iRow];
}

-(void)pressHeadImage
{
    if ([DataControl shareControl].currentFI != nil
        && [ViewManager sharedViewManager].currentVC ==
        (CommonViewController*)[ViewManager sharedViewManager].chatvc) {
        if ([[DataControl shareControl].currentFI.mail isEqualToString:
             [DataControl shareControl].personDS.uinfo.mail]) {
            [[ViewManager sharedViewManager] showView:VI_USERINFO
                                     withIsRemoveCurV:false
                                        withDirection:VD_RIGHT];
        }
        else
        {
            [[ViewManager sharedViewManager] showView:VI_PERSONINFO
                                     withIsRemoveCurV:false
                                        withDirection:VD_RIGHT];
        }
    }
}

@end
